package com.eastfair.user.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.auth.dto.OrganizationDTO;
import com.eastfair.auth.entity.MenuRole;
import com.eastfair.auth.entity.UserOrganization;
import com.eastfair.user.entity.User;
import com.eastfair.user.dto.UserSaveDTO;
import com.eastfair.user.dto.UserUpdateDTO;
import com.eastfair.user.dto.UserPageQuery;
import com.eastfair.user.enumeration.UserStatusEnum;
import com.eastfair.user.service.UserService;

import java.lang.reflect.Method;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 用户表
 * </p>
 *
 * @author ligang
 * @date 2023-09-09
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/user")
@Api(value = "User", tags = "用户表")
@PreAuth(replace = "user:user:")
public class UserController extends SuperController<UserService, Long, User, UserPageQuery, UserSaveDTO, UserUpdateDTO> {

    private static List<Long> allIds = new ArrayList<>();

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<User> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<User> userList = list.stream().map((map) -> {
            User user = User.builder().build();
            //TODO 请在这里完成转换
            return user;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(userList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<UserPageQuery> params) {
        UserPageQuery pageQuery = params.getModel();

    }



    /**
     * 查询子组织id
     *
     * @param
     * @return
     */
    @ApiOperation(value = "测试", notes = "测试")
    @GetMapping("/test")
    @SysLog("查询子组织id")
    public R test() {
        ContextUtil.setProjectId(45680859843526710L);
        User  user = new User();
        user.setName("333");
        user.setNickName("3334");
        user.setStatus(UserStatusEnum.WAIT_INIT);
        user.setHeadImg("11111111");
        baseService.executetest(user);
//        return R.success(baseService.save(user));
        return R.success(null);
    }



    /**
     * 随机查询
     *
     * @param
     * @return
     */
    @ApiOperation(value = "随机查询", notes = "随机查询")
    @GetMapping("/randowFind")
    public R randowFind() {
        ContextUtil.setProjectId(45680859843526710L);
        if(CollectionUtil.isEmpty(allIds)){
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("id");//指定查询某字段
            List<User> sysUsers=baseService.list(queryWrapper);
            allIds = sysUsers.stream().map(User::getId).collect(Collectors.toList());
        }
        User user =  baseService.getById(getRandomElement(allIds));

        Random random = new Random();
        int index = random.nextInt(allIds.size());
        List<Long> result = new ArrayList();
        for(int i =0 ;i<10 ;i++){
            result.add(allIds.get(index));
            index = random.nextInt(allIds.size());
        }
        List<User> list2 = baseService.listOfCache(null,result);

        List<User> list3 = baseService.listOfCache(null,Arrays.asList(50494934067577030L,50494934067577250L,50502806742671360L));

        return R.success(user.getId());
    }
    public static <T> T getRandomElement(List<T> list) {
        if (list == null || list.isEmpty()) {
            throw new IllegalArgumentException("List cannot be null or empty");
        }

        Random random = new Random();
        int index = random.nextInt(list.size());

        return list.get(index);
    }






}
